package org.itboys.core.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.core.entity.mongo.SundayAdvertisement;
import org.itboys.core.entity.mongo.SundayAdvertisementZone;
import org.itboys.core.service.mongo.web.SundayAdvertisementService;
import org.itboys.core.service.mongo.web.SundayAdvertisementZoneService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_广告_controller_web
 */
@Controller
@RequestMapping("/sunday/web/advertisement")
public class SundayAdvertisementController extends BaseController {

    @Autowired
    private SundayAdvertisementService advertisementService;
    @Autowired
    private SundayAdvertisementZoneService advertisementZoneService;


    /**---------------------------广告位信息--------------------------**/

    /**
     * 广告位主页
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/zone/index")
    public String zone_index(HttpServletRequest request,
                             Model model) {
        return "/sunday/advertisement/zone/index";
    }

    /**
     * 广告位。列表，分页获取
     *
     * @param request
     * @param response
     */
    @RequestMapping("/zone/select")
    public void zone_select(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        PageResult<SundayAdvertisementZone> result = advertisementZoneService.select(request);
        JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);
    }

    /**
     * 广告位。单个获取
     *
     * @param request
     * @param response
     */
    @RequestMapping("/zone/findOne")
    public void zone_findOne(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "id", required = true) Long id
    ) {
        SundayAdvertisementZone zone = advertisementZoneService.getById(id);
        ResponseMessage.success(zone);
    }

    /**
     * 新增或修改广告位
     *
     * @param request
     * @param response
     * @param zone
     */
    @RequestMapping("/zone/save")
    public void zone_save(
            HttpServletRequest request,
            HttpServletResponse response,
            @ModelAttribute SundayAdvertisementZone zone
    ) {
        advertisementZoneService.saveZone(zone);
        ResponseMessage.success();
    }

    /**
     * 删除广告位
     *
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/zone/delete")
    public void zone_delete(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "id", required = true) Long id
    ) {
        advertisementZoneService.deleteZone(id);
        ResponseMessage.success();
    }

    /**---------------------------广告信息--------------------------**/
    /**
     * 广告位
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(
            HttpServletRequest request,
            Model model,
            @RequestParam(value = "zoneKey", required = true) String zoneKey
    ) {
        model.addAttribute("zoneKey", zoneKey);
        return "/sunday/advertisement/index";
    }

    /**
     * 广告。列表，分页获取
     *
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        PageResult<SundayAdvertisement> result = advertisementService.select(request);
        JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);
    }

    /**
     * 新增或修正页面
     *
     * @param request
     * @param model
     * @param zoneKey
     * @return
     */
    @RequestMapping("/input")
    public String input(
            HttpServletRequest request,
            Model model,
            @RequestParam(value = "id", required = true) Long id,
            @RequestParam(value = "zoneKey", required = false) String zoneKey
    ) {
        SundayAdvertisement advertisement = advertisementService.findOne(id, zoneKey);
        model.addAttribute("advertisement", advertisement);
        return "/sunday/advertisement/input";
    }

    /**
     * 新增或修改广告
     *
     * @param request
     * @param response
     * @param Advertisement
     */
    @RequestMapping("/save")
    public void save(
            HttpServletRequest request,
            HttpServletResponse response,
            @ModelAttribute SundayAdvertisement Advertisement,
            @RequestParam(value = "imageFile", required = false) MultipartFile imageFile,
            @RequestParam(value = "videoFile", required = false) MultipartFile videoFile

    ) throws Exception {
        advertisementService.saveAdvertisement(Advertisement, imageFile, videoFile);
        ResponseMessage.success();
    }

    /**
     * 删除广告位
     *
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/delete")
    public void delete(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "id", required = true) Long id
    ) {
        advertisementService.delete(id);
        ResponseMessage.success();
    }
}
